<template>
  <el-container class="home_container">
    <el-header class="sky">
      <div class="clouds_one"></div>
      <div class="clouds_two"></div>
      <div class="clouds_three"></div>
      <div class="logo">
        <img src="../assets/avatar.jpg" alt="">
        <span>杭州20期后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggleBtn" @click="isCollapse = !isCollapse">|||</div>
        <el-menu active-text-color="#409eff" text-color="#fff" background-color="#373d41"
          :collapse="isCollapse" :collapse-transition="false" unique-opened
          :default-active="'/' + this.$route.path.split('/')[1]" router>
          <template v-for="item in menuList">
            <el-submenu :index="String(item.id)" :key="item.id">
              <template slot="title">
                <i :class="'iconfont icon-'+ menuIconList[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <template v-for="i in item.children">
                <el-menu-item :index="'/' + i.path" :key="i.id">
                  <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{i.authName}}</span>
                  </template>
                </el-menu-item>
              </template>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单列表
      menuList: [],
      // 左侧菜单对应的图标的class
      menuIconList: {
        125: 'users',
        103: 'tijikongjian',
        101: 'shangpin',
        102: 'danju',
        145: 'baobiao'
      },
      // 展开左侧菜单的开关
      isCollapse: false
    }
  },
  created() {
    // 调用获取菜单列表函数
    this.getMenus()
  },
  methods: {
    // 点击退出按钮后触发
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取菜单列表函数
    async getMenus() {
      const data = await this.$axios.get('/menus')
      if (data.meta.status !== 200) return this.$message.error('获取菜单列表失败！')
      this.menuList = data.data
    }
  }
}
</script>

<style scoped lang='less'>
.home_container {
  height: 100%;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
    background: #373d41;
    .logo {
      color: #fff;
      font-size: 25px;
      img {
        vertical-align: middle;
        width: 55px;
        height: 55px;
        border-radius: 50%;
      }
      span {
        margin-left: 10px;
      }
    }
  }
  .el-aside {
    background: #373d41;
    .el-menu {
      border: 0;
      .el-submenu {
        span {
          margin-left: 7px;
        }
      }
    }
    .toggleBtn {
      width: 100%;
      height: 40px;
      color: #fff;
      background: #373d41;
      text-align: center;
      line-height: 40px;
      letter-spacing: 5px;
      user-select: none;
      cursor: pointer;
    }
  }
  .el-main {
    background: #eaedf1;
  }
}
</style>
